/**
 *
 */
package de.cbf.cam.dsa.item;

import java.beans.IndexedPropertyChangeEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import de.cbf.cam.dsa.item.interfaces.ICompositeEdit;
import de.cbf.util.ui.listener.IUiChangeListener;
import de.cbf.util.ui.listener.UiChangeEvent;
import de.cbf.util.uuidheader.entities.UUIDHeader;

/**
 * @author salter
 *
 */
public abstract class AbstractControllerEdit extends AbstractController {

	protected void registerListenersAtView(ICompositeEdit composite) {
        composite.registerUIChangedListener(viewListener);
    }

    protected void unRegisterListenersAtView(ICompositeEdit composite) {
    	composite.unRegisterUIChangedListener(viewListener);
    }

    protected abstract void changePropertyInModel(int index);

    protected abstract void executeCommand(int index,Object... parameters);

    protected void registerModelListeners(UUIDHeader entity) {
		if(entity == null){
			return;
		}
		entity.getPropertyChangeSupport().addPropertyChangeListener(modelListener);
	}

	protected void unRegisterModelListeners(UUIDHeader entity) {
		if(entity == null){
			return;
		}
		entity.getPropertyChangeSupport().removePropertyChangeListener(modelListener);
	}

	protected abstract void updatePropertyInView(int index,Object newValue);



	protected EntityPCL modelListener = new EntityPCL();
	protected class EntityPCL implements PropertyChangeListener{
		@Override
		public void propertyChange(PropertyChangeEvent evt) {
			if (evt instanceof IndexedPropertyChangeEvent) {
				IndexedPropertyChangeEvent idx_Event = (IndexedPropertyChangeEvent) evt;
				updatePropertyInView(idx_Event.getIndex(),idx_Event.getNewValue());
			}
		}
	}

	protected CompositeUIL viewListener = new CompositeUIL();
	protected class CompositeUIL implements IUiChangeListener{
		@Override
		public void uiChanged(UiChangeEvent event) {
			int index = event.getIndex();
			changePropertyInModel(index);
		}

		@Override
		public void commandIssued(int index, Object... parameters) {
			executeCommand(index, parameters);
		}
	}

}
